package com.yobol.jgrowth.controller;

import com.yobol.jgrowth.commons.TagMap;
import com.yobol.jgrowth.domain.Article;
import com.yobol.jgrowth.domain.Tag;
import com.yobol.jgrowth.service.ArticleCommentService;
import com.yobol.jgrowth.service.ArticleService;
import com.yobol.jgrowth.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * Mangage article
 *
 * @author
 * @create 2018-04-01 21:25
 **/

@Controller
public class ArticleController {

    @Autowired
    private ArticleService articleService;
    @Autowired
    private ArticleCommentService articleCommentService;
    @Autowired
    private TagService tagService;
    @Autowired
    private TagMap tagMap;

    @RequestMapping(value = "/create_article",method = {RequestMethod.POST,RequestMethod.GET})
    public String createArticle(HttpServletRequest request, HttpServletResponse response) {
        String returnUrl = "/editor";
        String tag = (String)tagMap.getTagByIndex(request.getParameter("tag"));
        String articleName = request.getParameter("articleName");
        Integer uid = (Integer) request.getSession().getAttribute("uid");
        String description = request.getParameter("description");
        String articleContent = request.getParameter("articleContent");

        Article article = new Article();
        article.setName(articleName);
        article.setUid(uid);
        article.setDescription(description);
        article.setContent(articleContent);
        Tag articleTag = new Tag();
        articleService.addArticle(article);//result is insert status.Not aid.
        articleTag.setAid(article.getAid());
        articleTag.setTag(tag);
        tagService.addTag(articleTag);

//        request.getSession().setAttribute("tag",tag);
//        request.getSession().setAttribute("articles",articleService.getArticlesByTag(tag));
        String username = (String)request.getSession().getAttribute("username");
        request.getSession().setAttribute("tag",username + "的文章");
        request.getSession().setAttribute("articles",articleService.getArticlesByUsername(username));
        returnUrl = "/articles";

        return "redirect:" + returnUrl;
    }

    @RequestMapping(value = "/do_search",method = {RequestMethod.POST,RequestMethod.GET})
    public String search(HttpServletRequest request,HttpServletResponse response){
        String keywords = request.getParameter("keywords");
        request.getSession().setAttribute("tag",keywords);
        request.getSession().setAttribute("articles",articleService.getArticlesByKeywords(keywords));
        return "redirect:/articles";
    }

    @RequestMapping(value = "/get_articles_tag",method = {RequestMethod.POST,RequestMethod.GET})
    public String getArticlesByTag(HttpServletRequest request,HttpServletResponse response){
        String tag = request.getParameter("type");
        request.getSession().setAttribute("tag",tag);
        request.getSession().setAttribute("articles",articleService.getArticlesByTag(tag));
        return "redirect:/articles";
    }

    @RequestMapping(value = "/get_articles_username",method = {RequestMethod.POST,RequestMethod.GET})
    public String getArticlesByUid(HttpServletRequest request,HttpServletResponse response){

        String username = (String)request.getSession().getAttribute("username");
        request.getSession().setAttribute("tag",username + "的文章");
        request.getSession().setAttribute("articles",articleService.getArticlesByUsername(username));
        return "redirect:/articles";
    }

    @RequestMapping(value = "/get_article",method = {RequestMethod.POST,RequestMethod.GET})
    public String getArticle(HttpServletRequest request,HttpServletResponse response){

        int aid = Integer.parseInt(request.getParameter("aid"));
        Article article = articleService.getArticleByAid(aid);
        Article newArticle = new Article();
        newArticle.setAid(aid);
        newArticle.setScannedCtr(article.getScannedCtr() + 1);
        articleService.updateArticleScannedCtr(newArticle);
        request.getSession().setAttribute("article",article);
        request.getSession().setAttribute("comments",articleCommentService.getArticleCommentsByAid(aid));


        //previous and next article
        List<Article> articles = null;
        if (request.getSession().getAttribute("articles") != null){
            articles = (List<Article>)request.getSession().getAttribute("articles");
        }else{
            if (tagService.getTagByAid(aid).getTag().equals("Java语言基础")){
                articles = (List<Article>)request.getSession().getAttribute("javabasic");
            }else if (tagService.getTagByAid(aid).getTag().equals("Web")){
                articles = (List<Article>)request.getSession().getAttribute("web");
            }else if(tagService.getTagByAid(aid).getTag().equals("设计模式")){
                articles = (List<Article>)request.getSession().getAttribute("designpattern");
            }
        }
        int index = 0;
        for (;index < articles.size();index++ ){
            if (articles.get(index).getAid().equals(article.getAid())){
                break;
            }
        }
        Article previous = null;
        Article next = null;
        if (articles.size() > 1){
            previous = (index > 0) ? articles.get(index - 1) : null;
            next = (index < articles.size() - 1) ? articles.get(index + 1) : null;
        }
        request.getSession().setAttribute("previous",previous);
        request.getSession().setAttribute("next",next);

        return "redirect:/article";
    }

    @RequestMapping(value = "/modify_article",method = {RequestMethod.POST,RequestMethod.GET})
    public String modifyArticle(HttpServletRequest request,HttpServletResponse response){

        int aid = Integer.parseInt(request.getParameter("aid"));
        request.getSession().setAttribute("tag",tagService.getTagByAid(aid).getTag());
        request.getSession().setAttribute("article",articleService.getArticleByAid(aid));
        return "redirect:/editor?op=modify";
    }

    @RequestMapping(value = "/do_modify_article",method = {RequestMethod.POST,RequestMethod.GET})
    public String doModifyArticle(HttpServletRequest request,HttpServletResponse response){

        String tag = (String)tagMap.getTagByIndex(request.getParameter("tag"));
        int aid = Integer.parseInt(request.getParameter("aid"));
        String articleName = request.getParameter("articleName");
        String description = request.getParameter("description");
        String articleContent = request.getParameter("articleContent");

        Article article = new Article();
        article.setAid(aid);
        article.setName(articleName);
        article.setDescription(description);
        article.setContent(articleContent);
        articleService.updateArticleByPrimaryKey(article);
        Tag articleTag = new Tag();
        articleTag.setAid(article.getAid());
        articleTag.setTag(tag);
        tagService.updateTagByPrimaryKey(articleTag);
        String username = (String)request.getSession().getAttribute("username");
        request.getSession().setAttribute("tag",username + "的文章");
        request.getSession().setAttribute("articles",articleService.getArticlesByUsername(username));
        return "redirect:/articles";
    }

    @RequestMapping(value = "/delete_article",method = {RequestMethod.POST,RequestMethod.GET})
    public String deleteArticle(HttpServletRequest request,HttpServletResponse response){

        int aid = Integer.parseInt(request.getParameter("aid"));
        //delete article
        articleService.deleteArticleByAid(aid);
        //delete article comments associated with article
        articleCommentService.deleteArticleCommentsByAid(aid);
        //delete tags associated with article
        tagService.deleteTagByAid(aid);
        //delete notes associated with article

        String username = (String)request.getSession().getAttribute("username");
        request.getSession().setAttribute("tag",username + "的文章");
        request.getSession().setAttribute("articles",articleService.getArticlesByUsername(username));
        return "redirect:/articles";
    }

}
